//
//  RJWarningAQIView.m
//  AirQuality
//
//  Created by Po on 15/4/9.
//  Copyright (c) 2015年 Po. All rights reserved.
//

#import "RJWarningAQIView.h"
#import "RJWarningAQIDay.h"
#import "RJTools.h"

@interface RJWarningAQIView () <WarningAQIDayDelegate, RJWarningFatherDeleaget>

@property (retain, nonatomic) NSMutableArray * daysArray;

- (void)initInterface;
@end

@implementation RJWarningAQIView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self initInterface];
        [self setFatherDelegate:self];
    }
    return self;
}

- (void)initInterface
{
    _daysArray = [[NSMutableArray alloc] init];
    NSUInteger dayCount = 1;
    for (NSUInteger i = 0; i < 3 ; i ++) {
        RJWarningAQIDay * day = [[RJWarningAQIDay alloc] initWithFrame:CGRectMake(0, 160 *  i, ScreenWidth, 140)];
        [day setDelegate:self];
        [day.timeLabel setText:[RJTools getTimeWithFormat:@"MM月dd日" time:GetFutureSinceNow(dayCount)]];
        [self addSubview:day];
        [_daysArray addObject:day];
        dayCount ++;
    }
}

#pragma mark - RJWarningAQI
- (void)WarningFatherSetData:(NSArray *)data
{
    BOOL isDone = [RJUserData defaultRJUserData].isDone;
    BOOL isAudit = [RJUserData defaultRJUserData].isAudit;
    BOOL isHistory = [RJUserData defaultRJUserData].isHistory;
    if (isDone || isHistory) {
        NSArray * aqiCenters = data[1];
        NSArray * aqiRange = data[2];
        NSArray * aqiQulity = data[3];
        
        for (NSUInteger i = 0; i < _daysArray.count ; i ++) {
            RJWarningAQIDay * day = (RJWarningAQIDay *)(_daysArray[i]);
            day.rankLabel.text = aqiQulity[i];
            if (isAudit) {
                [day.aqiCenter setHidden:YES];
                [day.aqiMax setHidden:YES];
                [day.aqiMin setHidden:YES];
                [day.aqiFinalLineLabel setHidden:YES];
                day.aqiCenterLabel.text = aqiCenters[i];
                day.aqiFinalLabel.text = aqiRange[i];
            } else {
                day.aqiCenter.text = aqiCenters[i];
                NSString * aqiRangeString = aqiRange[i];
                //判断是否为空，否则取出数据
                if (![aqiRangeString isEqualToString:@""]) {
                    NSRange range = [aqiRangeString rangeOfString:@"~"];
                    NSString * minText = [aqiRangeString substringToIndex:range.location];
                    [day.aqiMin setText:minText];
                    NSString * maxText = [aqiRangeString substringFromIndex:range.location + 1];
                    [day.aqiMax setText:maxText];
                }
            }
        }
    }
}

- (void)WarningFatherSetUserData:(NSDictionary *)data
{
    NSArray * aqi = data[@"aqi_npo"];
    for (NSUInteger i = 0; i < _daysArray.count; i ++) {
        RJWarningAQIDay * day = _daysArray[i];
        if (aqi && ![aqi[i] isEqualToString:@""]) {
            day.aqiCenter.text = aqi[i];
            [self WarningDayAQI:day number:[aqi[i] floatValue]];
        }
    }
}

- (void)WarningFatherWillRemoveSelf
{
}

- (NSDictionary *)WarningFatherGetData
{
    NSMutableArray * aqi = [NSMutableArray array];
    NSMutableArray * aqi_npo = [NSMutableArray array];
    NSMutableArray * aqiQulity = [NSMutableArray array];
    for (RJWarningAQIDay * day in _daysArray) {
        [aqi_npo addObject:day.aqiCenter.text];
        [aqi addObject:[NSString stringWithFormat:@"%@~%@",day.aqiMin.text,day.aqiMax.text]];
        [aqiQulity addObject:day.rankLabel.text];
    }
    return @{@"aqi":aqi,@"aqi_npo":aqi_npo,@"quality":aqiQulity};
}

#pragma mark - WarningAQIDayDelegate
- (void)WarningDayAQI:(RJWarningAQIDay *)warningDayAQI number:(CGFloat)number
{
    NSUInteger mathNum = [RJTools getAQIMathNumber:number];
    NSInteger minAQI = number - mathNum;
    minAQI = (minAQI < 0 ? 0 : minAQI);
    NSInteger maxAQI = number + mathNum;
    [warningDayAQI.aqiMax setText:GetStringWithInt(maxAQI)];
    [warningDayAQI.aqiMin setText:GetStringWithInt(minAQI)];
    [warningDayAQI.rankLabel setText:[self getAQIRankWithMinNum:minAQI MaxNum:maxAQI]];
}

- (void)WarningDayAQI:(RJWarningAQIDay *)warningDayAQI numbers:(NSArray *)numbers
{
    [warningDayAQI.rankLabel setText:[self getAQIRankWithMinNum:[numbers[0] integerValue] MaxNum:[numbers[1] integerValue]]];
}

/**获取对应规则污染程度*/
- (NSString *)getAQIRankWithMinNum:(NSUInteger)min MaxNum:(NSUInteger)max
{
    NSMutableString * string = [[NSMutableString alloc] init];
    [string appendString:[RJTools getRankWithNumber:min]];
    NSString * maxRank = [RJTools getRankWithNumber:max];
    if (![string isEqualToString:maxRank]) {
        [string appendFormat:@"或%@",maxRank];
    }
    return string;
}


@end
